package com.telecom.cms.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;

import com.telecom.cms.dto.SysLocLatn;
import com.telecom.cms.dto.TwoId;
import com.telecom.cms.dao.SysLocLatnDAO;

import com.telecom.cms.util.Constant;

public class SysGetIdDAO {

	Dao dao = new DaoImpStd();

	String db = (new Constant()).getPara("DATABASE");

	public String findPrdInstId(String latnid,String accsnbr) {
		//Dao dao = new DaoImp();
		Constant cons=new Constant();
		String sql=cons.getPara("findPrdInstIdByAccsNbr");
		sql=cons.setPara(0, accsnbr);
		//String sql="select prd_inst_id from "+(new Constant()).getPara("FIND_PRDINST_BY_ACCS_TAB")+" where accs_nbr='"+accsnbr+"' QUALIFY ROW_NUMBER() over (PARTITION BY accs_nbr order by BILLING_CYCLE_ID DESC)=1";
		try {
			sql=(new Constant()).changeLatnId((new SysLocLatnDAO()).findById("latn_id", latnid).getLatn_Cd(), sql);
			ResultSet	rs2 = dao.select(sql);
			if(rs2.next()) {
				return rs2.getString(1);
			}
			if (rs2 != null) {

				rs2.close();
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			dao.closeconn();
		}
		return accsnbr;
	}
	
	public String findcustidbycert(String latnid,String certnbr) {
		//Dao dao = new DaoImp();
		String rs=certnbr;
		Constant cons=new Constant();
		
		String sql=cons.getPara("findcustidbycert");
		sql=cons.setPara(0, certnbr);
		//String sql=(new Constant()).getPara("DATABASE");
		//String sql="select cust_id from "+(new Constant()).getPara("FIND_ACCS_BY_CUST_TAB")+" where cert_nbr='"+certnbr.trim()+"' QUALIFY ROW_NUMBER() over (PARTITION BY cust_id order by BILLING_CYCLE_ID DESC)=1";
		try {
			sql=(new Constant()).changeLatnId((new SysLocLatnDAO()).findById("latn_id", latnid).getLatn_Cd(), sql);
			
			ResultSet	rs2 = dao.select(sql);
			if(rs2.next()) {
				rs=rs2.getString(1);
			}
			if (rs2 != null) {

				rs2.close();
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			dao.closeconn();
		}
		//System.out.println(rs);
		return rs;
	}
	
	public List findAccsNbrs(String latnid,String custid) {
		//Dao dao = new DaoImp();
		List result=new ArrayList();
		
        Constant cons=new Constant();
		String sql=cons.getPara("findAccsNbrsByCustId");
		sql=cons.setPara(0, custid);
		
		//String sql="select accs_nbr from "+(new Constant()).getPara("FIND_ACCS_BY_CUST_TAB")+" where cust_id='"+custid+"' QUALIFY ROW_NUMBER() over (PARTITION BY cust_id,prd_inst_id order by BILLING_CYCLE_ID DESC)=1";
		try {
			sql=(new Constant()).changeLatnId((new SysLocLatnDAO()).findById(latnid).getLatn_Cd(), sql);
			//System.out.println(sql);
			ResultSet	rs2 = dao.select(sql);

			while(rs2.next()) {
				result.add(rs2.getString(1));
			}
			if (rs2 != null) {

				rs2.close();
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			dao.closeconn();
		}
		return result;
	}
	
	public HashMap findAccsNbrsmap(String latnid,List allidlist,int from)
	{
	     HashMap result =new HashMap();
	     TwoId twoid=null;
	     int preread=Integer.parseInt(com.telecom.cms.util.Constant.getStPara("CONT_PRE_READ"));
	     for (int i=from;i<allidlist.size()&&i<preread+from;i++)
	     {
	    	 twoid=(TwoId)allidlist.get(i);
	    	 result.put(twoid, findAccsNbrs(latnid,twoid.getCustId()));
	     }
	     return result;
	}
	
}